<template>
	<el-dialog draggable v-model="dialogShow" :close-on-click-modal="false" width="60%" @close="onClose">
		<el-form ref="formRef" :rules="formRules" :model="formData" size="default" label-width="90px">
			<el-row :gutter="30">
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
					<el-form-item label="类型" prop="TypeId">
						<el-select v-model="formData.TypeId" style="width: 100%">
							<el-option :value="item.Id" :label="item.Name" v-for="item in types" :key="item.Id" />
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
					<el-form-item label="作者" prop="Author">
						<el-input v-model="formData.Author" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
					<el-form-item label="标题" prop="Title">
						<el-input v-model="formData.Title" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
					<el-form-item label="内容" prop="Content">
						<Editor v-model:get-html="formData.Content" />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="onClose" size="default">取 消</el-button>
				<el-button type="primary" @click="onSubmit" size="default">提交</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { Ref, ref, defineAsyncComponent } from 'vue';
import { Post } from '/@/common/http';
import EMsg from '/@/common/eMsg';
const Editor = defineAsyncComponent(() => import('/@/components/editor/index.vue'));
const dialogShow = ref(false);

const formRef = ref();
const formRules = ref({
	TypeId: [{ required: true, message: '类型不可为空', trigger: 'blur' }],
	Title: [{ required: true, message: '标题不可为空', trigger: 'blur' }],
});
const formData = ref({
	Id: 0,
	TypeId: null,
	Title: '',
	Content: '',
	Author: '',
});
const types: Ref = ref([]);
const onShow = (id: number, typelist: any[]) => {
	dialogShow.value = true;
	formData.value.Id = id;
	types.value = typelist;
	if (id > 0) {
		Post('TBusinessNews/Get', { Id: id }, (res: any) => {
			formData.value = res.Data;
		});
	}
};
const onClose = () => {
	formRef.value.resetFields();
	dialogShow.value = false;
};
const onSubmit = () => {
	formRef.value.validate((valid: any) => {
		if (valid) {
			if (formData.value.Id > 0) {
				Post('TBusinessNews/Edit', formData.value, (res: any) => {
					EMsg.success(res.Msg);
					onClose();
					emit('refresh');
				});
			} else {
				Post('TBusinessNews/Add', formData.value, (res: any) => {
					EMsg.success(res.Msg);
					onClose();
					emit('refresh');
				});
			}
		}
	});
};

const emit = defineEmits(['refresh']);
defineExpose({
	onShow,
});
</script>
